function reload() {
  chrome.tabs.getSelected(undefined, function(selectedTab){
    // Create a reload process, add it to a job, and start the job
    var process = {
      action:"reload", delay:0, tabIds:[selectedTab.id],
      comment:"Reload selected tab"
    }
    startJob({
      processes:[process], loop:true, delay:5000,
      comment:"Reload selected tab every 5 sec."
    });
  });
  chrome.contextMenus.update(radDisable, {"checked":false});
}
function disableTab() {
  chrome.tabs.getSelected(undefined, function(selectedTab){
    stopJob(undefined, selectedTab.id);
  });
  chrome.contextMenus.update(radReload, {"checked":false});
}

// Update context menus for tabs
function updateTabMenus(tabId) {
  var active = isTabInActiveJob(tabId);
  chrome.contextMenus.update(radReload, {"checked":active});
  chrome.contextMenus.update(radDisable, {"checked":!active});
}
// Update context menus on tab change
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo) {
  updateTabMenus(tabId);
});
// Update context menus on window change
chrome.windows.onFocusChanged.addListener(function(windowId) {
  if (windowId != -1) {
    chrome.tabs.getSelected(windowId, function(selectedTab){
      updateTabMenus(selectedTab.id);
    });
  }
});

// End jobs on tab close (also fired on tabs when a windows is closed)
chrome.tabs.onRemoved.addListener(function(tabId) {
  stopJob(undefined, tabId);
});

var parent = chrome.contextMenus.create({"title": "Chromatic"});
var radDisable = chrome.contextMenus.create({
  "title": "Disable for current tab",
  "type": "radio",
  "checked": true,
  "parentId": parent,
  "onclick":disableTab
});
var seperator1 = chrome.contextMenus.create({
  "type": "separator", "parentId": parent
});
var radReload = chrome.contextMenus.create({
  "title": "Reload page every 5 sec.",
  "type": "radio",
  "checked": false,
  "parentId": parent,
  "onclick": reload
});